package gwt.common;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.log4j.Logger;

public class SpringUtil {
	
	private static ApplicationContext context;
	private static EntityManager em;
	static Logger log = Logger.getLogger(SpringUtil.class);
	public static ApplicationContext initIOCFactory(){
		if ( context == null ){
			context = new ClassPathXmlApplicationContext(new String[] {"spring.xml","test.spring.xml"});
		}
		
		
		return context;
	}
	
	
	public static EntityManager initEntityManager(){
		if ( em == null ) {
			EntityManagerFactory emf = (EntityManagerFactory)SpringUtil.getBean("emf");
			em = emf.createEntityManager();
			log.info("EntityManager has been inited");
			
		}
		return em;
	}
	
	public static Object getBean(String id){
		return context.getBean(id);
	}

}
